﻿/*
	Copyright 2008 CATsoft Development GmbH

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/
/*
	Display Functions
*/
/***********************************************************************
 Show Task
***********************************************************************/
function showTasks(){
	var s,t;
	var today = new Date();
	var hm;
	var dateString;
	var showdate = (options.getValue("showdate") == "1" );
	var showtime = (options.getValue("showtime") == "1");
	var showtags = (options.getValue("showtags") == "1"); 
	var due;
	var days;

	if(inEdit)
		return;

	if (tasks.length == 0 ){
		if( RTM.isConnected() ) 
			info.innerText = INFO_NOTASKS
		else
		{
			info.innerText = INFO_NOCONNECTION
		}

		view.height = 60;
		info.visible = true;
		listDiv.visible = false;
	}
	else {
		info.visible = false;
		listDiv.visible = true;

		tasklist.removeAllElements();
		clearReminders();
		var j = 0;
		for( var i = 0; i < tasks.length; i++){

			due = tasks[i].due;
			hm = hourminutes(due, Time24);
			if (tasks[i].dueIn == -9999)
			    days = ""
			else
			    days = tasks[i].dueIn;
			
			if (tasks[i].has_due_time == 1 ){
						t = due.toLocaleString() + "\n" + tasks[i].name;
			    if (showtags){
						if(showtime)
			        s = hm + " " + tasks[i].tags + " " + tasks[i].name;
						else
							s = tasks[i].tags + " " + tasks[i].name;
					}
          else{
						if(showtime)
							s = hm + " " + tasks[i].name;
						else
							s = tasks[i].name;
					}
			}
			else {
			    if (showtags)
			        s = tasks[i].tags + " " + tasks[i].name
			    else
			        s = tasks[i].name;
				t = tasks[i].name;
			}
		
			if(showdate) // && (tasks[i].has_due_time == 1 ))
			{
			    if (tasks[i].dueIn == -9999)
			        dateString = ""
			    else {
			        var m = due.getMonth() + 1;
			        var d = due.getDate();
			        var y = due.getYear();
			        if (usDate)
			            dateString = m + "/" + d + "/" + y
			        else
			            dateString = d + "/" + m + "/" + y
			    }
    	}
    	else
			  dateString = "";
	
			AddItem( tasks[i].id, s, t, tasks[i].note, tasks[i].color, tasklist.width, days, i, (tasks[i].completed.length==0), tasks[i].smartList, tasks[i].priority, showdate, dateString );

			if ((tasks[i].has_due_time == 1)&&(due>today)){
				var ms = due.getTime() - today.getTime();
				if(ms<86400000) 
					tasks[i].timeoutToken = view.setTimeout( "setReminder(" + i + ")", ms);
				j++
			}
		}
	}
}

/***********************************************************************
 Add task item to display
***********************************************************************/
function AddItem(id, str, tooltip, note, color, width, days, task, open, smartList, priority, showdate, dateString) {
var	labelwidth;
var	daywidth;
var 	tip;
var	el;
var smart = smartList?1:0;

	if(showdate)
	{
		labelwidth = width - 75;
		daywith = 60;
	}
	else
	{
		labelwidth = width - 35;
		daywidth = 20;
	}

	tip = note ? note : tooltip;
	entry = "<item height=\"20\" width=\"" + width + "\" name=\"" + id + "\" background=\"" + color + "\" onclick=\"mainClick('" + task + "','" + smart + "')\" ondblclick=\"openTaskInBrowser(" + task + ")\">";
	if(open)
		entry = entry + "<img x=\"0\" y=\"0\" width=\"14\" height=\"16\" src=\"drop" + priority + ".png\"/>";
	entry = entry + "<label x=\"16\" name=\"disp\"  width=\"" + labelwidth + "\" color=\"#000000\" tooltip=\"" + tip + "\" font=\"Tahoma\" size=\"8\">" + str+ "</label><label x=\"" + (labelwidth + 15) + "\" width=\"" + daywidth + "\" color=\"#000000\" align=\"right\" font=\"Tahoma\" size=\"8\">";
	if(showdate) 
		entry = entry + dateString + "</label>";
	else
		entry = entry + days + "</label>";
	entry = entry + "</item>";
	el = tasklist.AppendElement( entry );

return 1;
}

/***********************************************************************
 Loading Lists
***********************************************************************/
function loadLists(selected){

		lblists.RemoveAllElements();

		for(var i = 0; i < lists.length; ++i) {
			var listItem = lists[i];
			entry = "<item height=\"20\" width=\"270\" name=\"" + listItem.id + "\">" + getcbx( listItem.name, listItem.id, listItem.position, 16, 16 );
			entry = entry + " <label x=\"25\" width=\"200\" name=\"li" + listItem.id + "\" color=\"#000000\" font=\"Tahoma\" size=\"10\">" + listItem.name + "</label>" + getdiv( listItem.id, 16, 16) + "</item>"
			lblists.AppendElement(entry);
		}
		lblists.selectedIndex = selected;
}

/***********************************************************************
 Div for list colors
***********************************************************************/
function getdiv( n, w, h ){
var s;
	s = "<div height=\"" + h + "\" width=\"" + w + "\" x=\"230\" y=\"0\" background=\"" + getListColor(n) + "\"/>";
	return s;
}

/***********************************************************************
 Checkbox
***********************************************************************/
function getcbx( n, i, p, w, h ){
var s;
var v = getListShow(i,p);
s = "<checkbox height=\"" + h + "\" name=\"cbx" + i + "\" width=\"" + w + "\" x=\"0\" y=\"0\" downImage=\"stock_images\\checkbox_down.png\" value=\"" + v + "\" ";
s = s + "image=\"stock_images\\checkbox_up.png\" overImage=\"stock_images\\checkbox_over.png\" caption=\"\" checkedDownImage=\"stock_images\\checkbox_checked_down.png\" ";
s = s + "checkedImage=\"stock_images\\checkbox_checked_up.png\" checkedOverImage=\"stock_images\\checkbox_checked_over.png\" onclick=\"setListShow('"+p+"',"+i+")\"/>";
return s;
}

/***********************************************************************
	Scrollbar
***********************************************************************/
function sbOnChange(){
	tasklist.height = tasks.length * tasklist.itemHeight;
	sb.max= tasks.length * tasklist.itemHeight - listDiv.height;
	if (sb.max<0)
	sb.max=0;
	tasklist.y=-sb.value;
}

/***********************************************************************
	Vew resizing
***********************************************************************/
function view_onsize() {
	contentArea.width = view.width - 4;
	contentArea.height = view.height - 4;
	listDiv.width = contentArea.width;
	listDiv.height = contentArea.height - listDiv.y - smartDiv.height - 4;

	tasklist.width = listDiv.width - 20 ;
	tasklist.height = listDiv.height + 40;
	sb.height = listDiv.height - 4;
	sb.x = tasklist.width + 1;

	tasklist.itemwidth = tasklist.width;

	header3.x = tasklist.width - 10;

	info.y = (contentArea.height - info.height) / 2;

	smartDiv.width = contentArea.width - 4;
	smartDiv.y = contentArea.height - 20;
	smartTask.width = smartDiv.width - 2 - smartSave.width;
	smartSave.x = smartDiv.width - 15;

	saveLabel.width = contentArea.width - 4;
	saveLabel.y = contentArea.height - 20;

	showTasks();
}

